#include <iostream>
#include <cstdio>
#include <vector>
#include <deque>

using std::vector;
using std::cin;
using std::cout;
using std::min;
using std::pair;
using std::deque;
using std::make_pair;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

void input(vector < vector <int> > &matrix, pair <int, int> &s, pair <int, int> &f) {
    int m, n;
    cin >> n >> m;
    cin >> s.first >> s.second;
    cin >> f.first >> f.second;
    --s.first, --s.second, --f.first, --f.second;
    matrix.resize(n);
    for (int i = 0; i < n; i++) {
        matrix[i].resize(m);
        for (int j = 0; j < m; j++) {
            cin >> matrix[i][j];
        }
    }
}

bool is_vertex(const pair <int, int> &a, int n, int m,
               const vector < vector <int> > &matrix, const vector < vector <int> > &dist) {
    return a.first >= 0 && a.second >= 0 && a.first < n && a.second < m &&
            !matrix[a.first][a.second] && dist[a.first][a.second] == -1;
}

void bfs(const pair <int, int> &s, const vector < vector <int> > &matrix,
         vector < vector <int> > &dist) {
    int n = matrix.size(), m = matrix[0].size();
    deque < pair <int, int> > Deque;
    dist[s.first][s.second] = 0;
    Deque.push_back(s);
    while (!Deque.empty()) {
        pair <int, int> k = Deque[0];
        Deque.pop_front();
        for (int i = 0; i < 4; i++) {
            pair <int, int> to = make_pair(k.first + dx[i], k.second + dy[i]);
            if(is_vertex(to, n, m, matrix, dist)) {
                dist[to.first][to.second] = dist[k.first][k.second] + 1;
                Deque.push_back(to);
            }
        }
    }
}

int solution(const vector < vector <int> > &matrix, pair <int, int> &s, pair <int, int> &f) {
    vector < vector <int> > dist;
    dist.resize(matrix.size());
    for (int i = 0; i < dist.size(); i++) {
        dist[i].resize(matrix[i].size(), -1);
    }
    bfs(s, matrix, dist);
    return dist[f.first][f.second];
}

void output(int answer) {
    if (answer == -1) {
        cout << "NO";
    } else {
        cout << answer;
    }
}

int main() {
    vector < vector <int> > matrix;
    pair <int, int> s, f;
    int answer;
    input(matrix, s, f);
    answer = solution(matrix, s, f);
    output(answer);
    return 0;
}
